package com.hangzhou.xyj.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.db.RecordDao;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.RecordModel;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.CommonTitleBarView;
import com.hangzhou.xyj.ui.widget.SelectMenuDialog;
import com.hangzhou.xyj.utils.ToastUtil;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by neo.duan on 2016/4/21.
 * 本地播放器
 */
public class LocalPlayActivity extends BasePlayActivity {
    private static final String TAG = "LocalPlayActivity";
    private RelativeLayout mLayoutDisc; //磁盘旋转的layout
    private CircleImageView mIvHead;
    private ImageView mIvPlay;
    private ImageView mIvPrevious; //上一首
    private ImageView mIvNext; //下一首
    private RotateAnimation mAnimation;

    private List<RecordModel> mModelList; //本页面所有录制文件的集合
    private RecordModel mRecordModel; //本页面的录制文件数据结构
    private int mDramaPos; //当前播放的戏曲位置

    @Override
    public void initIntentData(Intent intent) {
        super.initIntentData(intent);
        mModelList = (List<RecordModel>) intent.getSerializableExtra(DataKey.DRAMA_LIST);
        mDramaPos = intent.getIntExtra(DataKey.DRAMA_POSITION, -1);
    }

    @Override
    public void initTop() {
        enableRightNav(true, R.drawable.ic_action_menu);
        setOnNavRightListener(new CommonTitleBarView.OnNavRightListener() {
            @Override
            public void onNavRight() {
                showMenuDialog();
            }
        });
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.layout_local_play);
    }

    @Override
    public void initViews() {
        mLayoutDisc = findView(R.id.layout_local_play_disc);
        mIvHead = findView(R.id.iv_local_play_head);
        mIvPlay = findView(R.id.iv_drama_playing);
        mIvPrevious = findView(R.id.iv_drama_play_previous);
        mIvNext = findView(R.id.iv_drama_play_next);
    }

    @Override
    public void initDatas() {
        if (mModelList != null && mModelList.size() > mDramaPos) {
            mRecordModel = mModelList.get(mDramaPos);
            if (mRecordModel != null) {
                enableTitle(true, mRecordModel.getName());
                mIvHead.displayImage(mRecordModel.getHead());
            }
        }
    }

    /**
     * 一进去就播放
     */
    @Override
    protected void onStart() {
        super.onStart();
        if (!hasPlaying()) {
            if (mRecordModel != null) {
                startRotate();
                startPlayback(mRecordModel.getMixerFilePath());
                mIvPlay.setImageResource(R.drawable.ic_drama_pause);
            }
        }
    }

    @Override
    public void initListeners() {
        mIvPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!hasPlaying()) {
                    //开始播放
                    startPlay();
                } else {
                    stopPlay();
                }
            }
        });

        //上一首
        mIvPrevious.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDramaPos == 0) {
                    //没有上一首,取最后一首
                    mDramaPos = mModelList.size() - 1;
                } else {
                    mDramaPos--;
                }
                initDatas();
                startPlay();
            }
        });

        //下一首
        mIvNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDramaPos == mModelList.size() - 1) {
                    //没有了下一首，取第一首
                    mDramaPos = 0;
                } else {
                    mDramaPos++;
                }
                initDatas();
                startPlay();
            }
        });
    }

    private void startPlay() {
        if (mRecordModel != null) {
            startRotate();
            startPlayback(mRecordModel.getMixerFilePath());
            mIvPlay.setImageResource(R.drawable.ic_drama_pause);
        }
    }

    private void stopPlay() {
        stopRotate();
        //暂停播放
        stopPlayback();
        mIvPlay.setImageResource(R.drawable.ic_drama_playing);
    }

    /**
     * 显示操作菜单对话框
     */
    private void showMenuDialog() {
        SelectMenuDialog dialog = new SelectMenuDialog(mContext);
        dialog.setMenu(new String[]{
                "上传", "删除"
        }, new SelectMenuDialog.SelectMenuClickListener() {
            //按钮添加点击事件
            @Override
            public void onSelectMenuClick(Dialog dialog, View item, int position) {
                switch (position) {
                    case 0:
                        IntentManager.getInstance().goPublishRecordActivity(mContext, mRecordModel);
                        break;
                    case 1:
                        deleteRecord(mRecordModel);
                        break;
                }
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    private void startRotate() {
        /** 设置旋转动画 */
        if (mAnimation == null) {
            mAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
                    0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            mAnimation.setDuration(20000);//设置动画持续时间
            mAnimation.setInterpolator(new LinearInterpolator());
            mAnimation.setRepeatCount(Integer.MAX_VALUE);//设置重复次数
            mAnimation.setFillBefore(true);
            mAnimation.setFillAfter(true);//动画执行完后是否停留在执行完的状态
        }
        mLayoutDisc.startAnimation(mAnimation);
    }

    private void stopRotate() {
        mLayoutDisc.clearAnimation();
    }

    /**
     * 删除该录制
     *
     * @param model
     */
    private void deleteRecord(RecordModel model) {
        if (model == null) {
            ToastUtil.show("删除失败,文件不存在");
        }
        RecordDao dao = new RecordDao(mContext);
        try {
            dao.delete(model);
        } catch (SQLException e) {
            e.printStackTrace();
            ToastUtil.show("删除失败");
        }
    }
}
